package com.wang.utils;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Component
public class FastDFSClient {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    public String upload(MultipartFile file) throws IOException {
        /*InputStream var1,
        long var2,
        String var4,
        Set<MetaData> var5
        * */
        //获取文件原来的名字
        String originalFilename = file.getOriginalFilename();
        //获取文件扩展名
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i + 1);
        //获取后缀名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        //storepath :group1/moo/56/ff 文件名
        String fullPath = storePath.getFullPath();
        String resAccessUrl = getResAccessUrl(storePath);
        return resAccessUrl;
    }

    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        return fileUrl;
    }








}
